package com.thinkfit.jy.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Debug;

public class MemoryInfoUtils {

    static MemoryInfoUtils memoryInfoUtils;

    public static MemoryInfoUtils getInstance(){
        if(memoryInfoUtils == null){
            memoryInfoUtils = new MemoryInfoUtils();
        }
        return memoryInfoUtils;
    }

    public String init(Context context){
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

        // 该设备整体内存情况
        ActivityManager.MemoryInfo systemMemoryInfo = new ActivityManager.MemoryInfo();
        activityManager.getMemoryInfo(systemMemoryInfo);
        long sysTotalMemory = systemMemoryInfo.totalMem / (1024 * 1024);//系统总内存
        long sysAvailableMemory = systemMemoryInfo.availMem / (1024 * 1024);//系统可用内存
        // 系统内存阈值，低于这个阈值，系统就要进行清理后台进程等操作
        long sysThreshold = systemMemoryInfo.threshold / (1024 * 1024);//系统检验阈值
        boolean sysNowIsLowMemory = systemMemoryInfo.lowMemory;//系统现在是否是低内存
//        Log.d("MemoryInfo", "system Total Memory: " + sysTotalMemory  + " MB");
//        Log.d("MemoryInfo", "system Available Memory: " +sysAvailableMemory + " MB");
//        Log.d("MemoryInfo","system Threshold: " + sysThreshold + " MB");
//        Log.d("MemoryInfo", "system now is Low Memory: " + sysNowIsLowMemory);

// 对每个App的java堆限制
        int largeMemoryClass = activityManager.getLargeMemoryClass();
//        Log.d("MemoryInfo", "设置android:largeHeap=\"true\" , App Max memory is " + largeMemoryClass + "MB");
        int memoryClass = activityManager.getMemoryClass();
//        Log.d("MemoryInfo", "App max memory is " + memoryClass + "MB");

        // 获取本进程使用的内存情况值
        int pid = android.os.Process.myPid();
        int[] pids = new int[]{pid};
        Debug.MemoryInfo[] memoryInfoArray = activityManager.getProcessMemoryInfo(pids);
        Debug.MemoryInfo memoryInfo = memoryInfoArray[0];
        // 获取总内存使用 它等于dalvikPss + nativePss + otherPss + TotalSwappedOutPss
        int totalPss = memoryInfo.getTotalPss()/1024;
        int totalPrivateDirty = memoryInfo.getTotalPrivateDirty()/1024;
        int totalSharedDirty = memoryInfo.getTotalSharedDirty()/1024;

        // 获取 Native 堆内存
        int nativePss = memoryInfo.nativePss/1024;
        int nativePrivateDirty = memoryInfo.nativePrivateDirty/1024;
        int nativeSharedDirty = memoryInfo.nativeSharedDirty/1024;

        // 获取 Dalvik 堆内存
        int dalvikPss = memoryInfo.dalvikPss/1024;
        int dalvikPrivateDirty = memoryInfo.dalvikPrivateDirty/1024;
        int dalvikSharedDirty = memoryInfo.dalvikSharedDirty/1024;

//        Log.d("MemoryInfo", "Total PSS: " + totalPss + " MB");
//        Log.d("MemoryInfo", "Total Private Dirty: " + totalPrivateDirty + " MB");
//        Log.d("MemoryInfo", "Total Shared Dirty: " + totalSharedDirty + " MB");
//
//        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa  Native PSS: " + nativePss + " MB");
//        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa  Native Private Dirty: " + nativePrivateDirty + " MB");
//        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa  Native Shared Dirty: " + nativeSharedDirty + " MB");
//
//        Log.d("MemoryInfo", "Dalvik PSS: " + dalvikPss + " MB");
//        Log.d("MemoryInfo", "Dalvik Private Dirty: " + dalvikPrivateDirty + " MB");
//        Log.d("MemoryInfo", "Dalvik Shared Dirty: " + dalvikSharedDirty + " MB");
        return (sysTotalMemory-sysAvailableMemory)+" MB";
    }
}
